package HeSuanServer.service.Impl;

import HeSuanServer.POJO.*;
import HeSuanServer.POJO.R.Result;
import HeSuanServer.POJO.R.StatusCode;
import HeSuanServer.mapper.OrgMapper;
import HeSuanServer.service.OrgService;
import cn.hutool.core.date.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @描述:
 * @author: LH
 * @date: 2023/4/7
 */
@Service
public class OrgServiceImpl implements OrgService {
    @Autowired
    private OrgMapper orgMapper;
    @Override
    public Result getAll() {
        try {
            List<Map> data=new ArrayList<>();
            List<Org> list= orgMapper.getAll();
            for (Org org : list) {
                Map<String,Object> map=new HashMap();
                map.put("orgId",org.getId());
                map.put("orgName",org.getOrgName());
                data.add(map);
            }
            return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(data);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
        }
    }

    @Override
    public Result findByPage(OrgSearch orgSearch) {
        try {
            if (orgSearch.getOrg()==null) {
                orgSearch.setOrg("");
            }
            Integer current = orgSearch.getCurrent();
            Integer size = orgSearch.getSize();
            Map<String,Object> map=new HashMap<>();
            map.put("start",(current-1)*size);
            map.put("end",size);
            map.put("p",orgSearch.getOrg());
            List<Org> list=orgMapper.getByPage(map);
            Integer count=orgMapper.getConut(map);
            Map<String,Object> data=new HashMap<>();
            data.put("list",list);
            data.put("total",count);
            return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(data);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
        }
    }

    @Override
    public Result byId(Integer id) {
        try {
            Org org=orgMapper.byId(id);
            return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(org);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
        }
    }

    @Override
    public Result edit(Org org) {
        try {
            orgMapper.edit(org);
            return Result.Builder().StatusCode(StatusCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_UPDATE);
        }
    }

    @Override
    public Result add(Org org) {
        try {
            orgMapper.add(org);
            return Result.Builder().StatusCode(StatusCode.SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_INSERT);
        }
    }

    @Override
    public Result delById(Integer id) {
        try {
            orgMapper.delById(id);
            return Result.Builder().StatusCode(StatusCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_UPDATE);
        }
    }

    @Override
    public Result sq(Rorg rorg) {
        try{
            rorg.setStatus("审核中");
            orgMapper.sq(rorg);
            return Result.Builder().StatusCode(StatusCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_INSERT);
        }
    }

    @Override
    public Result login(Org org) {
        try {
            Org o=orgMapper.login(org);
            if (o==null){
                return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
            }
            return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(o);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
        }
    }

    @Override
    public Result gety(int id) {
        try {
            Map<String,Object> map=new HashMap<>();
            getToday(id, map);
            int count=orgMapper.getY(map);
            return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(count);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
        }
    }

    private void getToday(int id, Map<String, Object> map) {
        map.put("id", id);
        DateTime start=new DateTime();
        start.setSeconds(0);
        start.setHours(0);
        start.setMinutes(0);
        map.put("start",start);
        DateTime end=new DateTime();
        end.setSeconds(59);
        end.setHours(23);
        end.setMinutes(59);
        map.put("end",end);
    }

    @Override
    public Result totaly(Integer id) {
        try{
            Map<String,Object> map=new HashMap<>();
            getToday(id, map);
            int count=orgMapper.totaly(map);
            return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(count);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT_COUNT);
        }
    }

    @Override
    public Result bili(Integer id) {
        try {
            Map<String,Object> map=new HashMap<>();
            getToday(id, map);
            int y=orgMapper.getY(map);
            int total=orgMapper.totaly(map);
            if(total==0){
                return Result.Builder().StatusCode(StatusCode.SUCCESS).Data("无数据");
            }else {
                int data=y*100/total;
                return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(data+"%");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
        }

    }

    @Override
    public Result wt(Integer id) {
        try {
            Map<String,Object> map=new HashMap<>();
            getToday(id, map);
            int y=orgMapper.getY(map);
            int total=orgMapper.totaly(map);
            return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(total-y);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
        }
    }

    @Override
    public Result tj(Integer id) {
        try{
           Map<String,Object> map=new HashMap<>();
            //        获取前七天的日期
            List<String> qrdate=new ArrayList<>();
            SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
            for (int i = 6; i >= 0; i--) {
                String str = dateFormat.format(new DateTime().getTime()- 1000L *60*60*24*i);
                qrdate.add(str);
            }
            map.put("qrdate",qrdate);
            //        获取前七天数据
            List<Integer> qrnumber= new ArrayList<>();
            for (int i = 6; i >= 0; i--) {
                Map<String,Object> o = getTime(i);
                o.put("id",id);
                Integer y = orgMapper.getY(o);
                qrnumber.add(y);
            }
            map.put("qrnumber",qrnumber);
            return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(map);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
        }
    }

    @Override
    public Result UmSearch(OrgUmSearch orgUmSearch) {
        try {
            Map<String,Object> map=new HashMap<>();
            map.put("r",orgUmSearch.getStatus());
//            日期设置
            if(orgUmSearch.getDate()!=null){
                DateTime start = new DateTime(orgUmSearch.getDate());
                start.setSeconds(0);
                start.setHours(0);
                start.setMinutes(0);
                map.put("start",start);
                DateTime end=new DateTime(orgUmSearch.getDate());
                end.setSeconds(59);
                end.setHours(23);
                end.setMinutes(59);
                map.put("end",end);
            }
            map.put("name",orgUmSearch.getUser());
            map.put("id",orgUmSearch.getOrgId());
            Integer currentPage = orgUmSearch.getCurrentPage();
            int page1=(currentPage-1)*8;
            int page2=8;
            map.put("page1",page1);
            map.put("page2",page2);
            List<UserMessage> list=orgMapper.UmSearch(map);
//           结果过滤
            for (UserMessage userMessage : list) {
                switch (userMessage.getResult()){
                    case "0": userMessage.setResult("检测中");break;
                    case "1": userMessage.setResult("阴性");break;
                    case "2": userMessage.setResult("阳性");break;
                }
            }
            Map<String,Object> date=new HashMap<>();
            date.put("list",list);
            date.put("total",orgMapper.UmSearchCount(map));
            return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(date);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
        }
    }

    @Override
    public Result orgedit(Org org) {
        try {
            orgMapper.orgedit(org);
            return Result.Builder().StatusCode(StatusCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_UPDATE);
        }
    }

    @Override
    public Result getRorg(OrgSearch orgSearch) {
        try{
            String orgName = orgSearch.getOrg();
            Map<String,Object> map=new HashMap<>();
            map.put("orgName",orgName);
            map.put("start",(orgSearch.getCurrent()-1)*orgSearch.getSize());
            map.put("end",(orgSearch.getCurrent()*orgSearch.getSize()));
            List<Rorg> list=orgMapper.getRorg(map);
            Integer count=orgMapper.getRorgCount(map);
            Map<String,Object> data=new HashMap<>();
            data.put("list",list);
            data.put("total",count);
            return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(data);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
        }
    }

    @Override
    public Result tg(Map<String, String> map) {
        try {
            Org org1=orgMapper.getOrg(map);
            if (org1!=null){
                return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT).Msg("账户已被使用");
            }
            Rorg r=orgMapper.rorgById(Integer.parseInt(map.get("id")));
            orgMapper.delRorg(Integer.parseInt(map.get("id")));
            Org org=new Org();
            org.setOrgName(r.getOrgName());
            org.setOrgPassword(map.get("password"));
            org.setOrgUser(map.get("user"));
            org.setAddress(r.getAddress());
            org.setNumber(r.getLxNumber());
            org.setLianxi(r.getLxName());
            orgMapper.add(org);
            System.out.println("手机号："+org.getNumber()+"-------"+"账号为"+map.get("user")+"---密码为："+map.get("password"));
            return Result.Builder().StatusCode(StatusCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
        }

    }

    @Override
    public Result rdel(Integer id) {
        try {
            orgMapper.delRorg(id);
            return Result.Builder().StatusCode(StatusCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_UPDATE);
        }
    }

    public Map<String,Object> getTime(int i){
        DateTime date=new DateTime(new DateTime().getTime()- 1000L *60*60*24*i);
        date.setSeconds(0);
        date.setHours(0);
        date.setMinutes(0);
        Map<String,Object> map=new HashMap<>();
        map.put("start",date);
        DateTime end=new DateTime(new DateTime().getTime()- 1000L *60*60*24*i);
        end.setSeconds(59);
        end.setHours(23);
        end.setMinutes(59);
        map.put("end",end);
        return map;
    }
}
